package cc.idoone.spring_boot_hello.interceptors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * HandlerInterceptor 测试
 *
 * @author hemj
 * @since 2017/10/22
 */
public class LogInterceptor implements HandlerInterceptor {

	private static final Logger logger = LoggerFactory.getLogger(LogInterceptor.class);

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		logger.info("[{}] [{}] [{}] [{}] [{}] [{}]",
				"preHandle",
				request.getMethod(),
				request.getRequestURI(),
				request.getQueryString(),
				request.getRemoteAddr(),
				request.getHeader("User-Agent"));
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest httpServletRequest,
						   HttpServletResponse httpServletResponse, Object handler, ModelAndView modelAndView) throws Exception {
		logger.info("postHandle");
	}

	@Override
	public void afterCompletion(HttpServletRequest httpServletRequest,
								HttpServletResponse httpServletResponse, Object handler, Exception exception) throws Exception {
		logger.info("afterCompletion");
		if (exception != null) {
			logger.error("logger", exception);
		}
	}
}

